package com.jit.monitor.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jit.baselib.util.image.GlideLoaderUtils;
import com.jit.baselib.util.ui.ResHelper;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.baselib.util.ui.ViewHelper;
import com.jit.monitor.R;
import com.jit.monitor.model.CheckDataManager;
import com.jit.monitor.model.response.MainResponse;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @author crazyZhangxl on 2018/12/21.
 * Describe:
 */
public class MainListAdapter extends BaseQuickAdapter<MainResponse,BaseViewHolder>{
    private DetailDetInfoListener mDetailDetInfoListener;
    private Context mmContext;
    public void setDetailDetInfoListener(DetailDetInfoListener detailDetInfoListener){
        this.mDetailDetInfoListener = detailDetInfoListener;
    }

    public MainListAdapter(Context context, int layoutResId, @Nullable List<MainResponse> data) {
        super(layoutResId, data);
        mmContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, MainResponse item) {
        /*设置常规的属性*/
        helper.setText(R.id.tvIP,"Ip地址:"+item.getCamera().getId());
        helper.setText(R.id.tvCameraName,"名称:"+item.getCamera().getName());
        LinearLayout llIdentify = helper.getView(R.id.llIdentify);
        ImageView iv = helper.getView(R.id.ivFrame);
        /*--------加载图片-------*/
        if (iv == null){
            Log.e(TAG, "-------笑死了--------" );
        }
        if (item.getFrameInfo().getImage() != null) {
            Log.e(TAG, "-------笑死了--------" );
            Glide.with(mmContext).load(item.getFrameInfo().getImage()).into(iv);
        }
        Object state = item.getCamera().getState();
        if (state != null) {
            helper.setText(R.id.tvState, state.toString());
        }else {
            helper.setText(R.id.tvState,"无状态");
        }

        /*
         * 设置编辑以及图片的点击时间 ------------
         */
//        helper.addOnClickListener(R.id.rlImage)
//                .addOnClickListener(R.id.frameImage);
        List<MainResponse.ObjTypeOutListBean> objTypeOutList = item.getObjTypeOutList();
        TagFlowLayout mFlowLayout =  helper.getView(R.id.tagFlowLayout);
        if (objTypeOutList == null || objTypeOutList.size() == 0){
            llIdentify.setVisibility(View.GONE);
        }else {
            List<String> mValues = new ArrayList<>();
            mValues.clear();
            for (int i=0;i<objTypeOutList.size();i++){
                mValues.add(CheckDataManager.getCheckChineseName(objTypeOutList.get(i).getObjType())+" "+objTypeOutList.get(i).getTypeNum());
            }
            mFlowLayout.setAdapter(new TagAdapter<String>(mValues) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_tv,
                            parent, false);
                    GradientDrawable mGradientDrawable = new GradientDrawable();
                    mGradientDrawable.setCornerRadius(10);
                    mGradientDrawable.setStroke(1,CheckDataManager.getColor(position));
                    tv.setBackground(mGradientDrawable);
                    tv.setText(s);
                    return tv;
                }
            });

            mFlowLayout.setOnTagClickListener((view, position, parent) -> {
                if (mDetailDetInfoListener != null){
                    // 设置点击时间 -------------
                    mDetailDetInfoListener.onDetSelected(item.getFrameInfo().getId(),item.getObjTypeOutList().get(position).getObjType());
                }
                return false;
            });
        }
    }

    public interface DetailDetInfoListener{
        /**
         * 选择具体的识别-----
         * @param frameID
         * @param objDetType
         */
        void onDetSelected(String frameID,String objDetType);
    }
}
